package com.tangula.web.kashine.controller;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.tangula.controller.ReturnRespController;
import com.tangula.core.Context;
import com.tangula.core.Controller;
import com.tangula.web.config.auth.Ignore;
import com.tangula.web.form.DefaultPagingBody;
import com.tangula.web.kashine.business.OrgBusiness;
import com.tangula.web.kashine.business.ProductBusiness;
import com.tangula.web.kashine.business.StoreBusiness;
import com.tangula.web.kashine.business.StudentBusiness;
import com.tangula.web.kashine.business.SupportBusiness;
import com.tangula.web.kashine.business.TeacherBusiness;
import com.tangula.web.kashine.entity.AccountEntity;
import com.tangula.web.kashine.entity.AddTeacher2OrgEntity;
import com.tangula.web.kashine.entity.BaseRespEntity;
import com.tangula.web.kashine.entity.FreeTeacherModule;
import com.tangula.web.kashine.entity.LoadOrgStoreForm;
import com.tangula.web.kashine.entity.LoadOrgStudentForm;
import com.tangula.web.kashine.entity.LoadOrgTeacherForm;
import com.tangula.web.kashine.entity.LoadProductOfOrgStoreForm;
import com.tangula.web.kashine.entity.OrgForm;
import com.tangula.web.kashine.entity.OrgProductForm;
import com.tangula.web.kashine.entity.OrgStudentForm;
import com.tangula.web.kashine.entity.OrgTeacherForm;
import com.tangula.web.kashine.entity.PagableForm;
import com.tangula.web.kashine.entity.ProductEntity;
import com.tangula.web.kashine.entity.SaleProducesForm;
import com.tangula.web.kashine.entity.SendMoneyToTeacherForm;
import com.tangula.web.kashine.entity.StoreEntity;
import com.tangula.web.kashine.entity.StudentAccountForm;
import com.tangula.web.kashine.entity.SupportEntity;
import com.tangula.web.kashine.entity.TeacherForm;






@RestController
@RequestMapping("/orgController")
@ResponseBody
public class OrgController  implements Controller<Context>, ReturnRespController,AuthController{
	@Autowired
	OrgBusiness orgBusiness;
	@Autowired
	TeacherBusiness teacherBusiness;
	@Autowired
	StudentBusiness studentBusiness;
	@Autowired
	StoreBusiness storeBusiness;
	@Autowired
	ProductBusiness productBusiness;
	@Autowired
	SupportBusiness supportBusiness;
	/**
	 * 组织注册
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/postRegister",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> postRegister(@RequestBody AccountEntity account){
		return returnSuccess(teacherBusiness.orgRegister(account)!=null);
	}
	/**
	 * 组织展示教师
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadOrgTeachers",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<AccountEntity>>> loadOrgTeachers(@RequestBody LoadOrgTeacherForm loadOrgTeacherForm){
		return returnSuccess(teacherBusiness.loadOrgTeachers(loadOrgTeacherForm));
	}
	/**
	 * 组织删除教师
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/deleteRemoveTeacherFromOrg",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> deleteRemoveTeacherFromOrg(@RequestBody TeacherForm account){
		return returnSuccess(orgBusiness.removeOrgTeacher(account)!=null);
	}
	/**
	 * 组织查看没有组织的教师
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadNoOrgTeacher",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<AccountEntity>>> putTeacherIntoOrg(@RequestBody PagableForm pag){
		return returnSuccess(orgBusiness.showNoOrgTeacher(pag));
	}
	/**
	 * 组织添加教师
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putTeacherIntoOrg",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putTeacherIntoOrg(@RequestBody OrgTeacherForm orgteacher){
		return returnSuccess(orgBusiness.addOrgTeacher(orgteacher)!=null);
	}
	/**
	 * 组织展示学生
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadOrgStudents",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<AccountEntity>>> loadOrgStudents(@RequestBody LoadOrgStudentForm form){
		form.setOrgId(loadCurrentAccount().getId());
		return returnSuccess(studentBusiness.loadOrgStudentsOfOrg(form));
	}
	
	/**
	 * 组织删除学生
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/deleteRemoveStudentFromOrg",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> deleteRemoveStudentFromOrg(@RequestBody String studentId){
		return returnSuccess(orgBusiness.removeOrgStudent(studentId,loadCurrentAccount().getId()));
	}
	
	/**
	 * 组织删除学生
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/removeStudentFromOrg",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> removeStudentFromOrg(@RequestBody Map<String, Object> from){
		return returnSuccess(orgBusiness.removeOrgStudentById(loadCurrentAccount().getId(), (String)from.get("studentId")));
	}
	
	/**
	 * 组织添加学生
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putStudentIntoOrg",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putStudentIntoOrg(@RequestBody OrgStudentForm orgstudent){
		orgstudent.setOrgId(loadCurrentAccount().getId());
		return returnSuccess(orgBusiness.addOrgStudent(orgstudent));
	}
	/**
	 * 组织展示商店
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/postLoadOrgStore",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<StoreEntity>>> postLoadOrgStore(@RequestBody LoadOrgStoreForm loadOrgStoreFrom){
		loadOrgStoreFrom.setCondition(loadCurrentAccount().getId());
		return returnSuccess(storeBusiness.loadOrgStore(loadOrgStoreFrom));
	}
	/**
	 * 组织添加商店
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/putStoreIntoOrg",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putStoreIntoOrg(@RequestBody StoreEntity store){
		store.setOwnerOrganization(loadCurrentAccount().getId());
		return returnSuccess(storeBusiness.addOrgStore(store)!=null);
	}
	/**
	 * 组织删除商店
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/deleteRemoveStoreFromOrg",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> deleteRemoveStoreFromOrg(@RequestBody StoreEntity store){
		return returnSuccess(storeBusiness.removeStore(store)!=null);
	}
	/**
	 * 组织展示商品
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadOrgProducts",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<OrgProductForm>>> postLoadProductsOfOrgStore(@RequestBody LoadProductOfOrgStoreForm loadPro){
		return returnSuccess(productBusiness.loadProductsOfOrgStore(loadPro));
	}
	/**
	 * 组织删除商品
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/removeProductsFromOrgStore",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> removeProductsFromOrgStore(@RequestBody ProductEntity product){
		return returnSuccess(productBusiness.removeProductsFromOrgStore(product)!=null);
	}
	/**
	 * 组织添加商品
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/creatProdInOrg",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> putProductIntoOrgStore(@RequestBody ProductEntity product){
		return returnSuccess(productBusiness.addOrgStoreProduct(product)!=null);
	}
	/**
	 * 组织发送基金给教师
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/postAssignCash",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> postAssignCash(@RequestBody SendMoneyToTeacherForm send){
		return returnSuccess(orgBusiness.assignCash(send)!=null);
	}
	/**
	 * 组织更改状态为不可被查
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/postChangeStatusToN",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> postChangeStatusToN(@RequestBody OrgForm org){
		return returnSuccess(orgBusiness.changeStatusToN(org)!=null);
	}
	/**
	 * 组织更改状态为可被查
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/postChangeStatusToY",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> postChangeStatusToY(@RequestBody OrgForm org){
		return returnSuccess(orgBusiness.changeStatusToY(org)!=null);
	}
	/**
	 * 组织support
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/postsupport",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> postsupport(@RequestBody SupportEntity support){
		return returnSuccess(supportBusiness.saveSupport(support)!=null);
	}

	
	/**
	 * 组织查询所有没加入组织的学生
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadAllFreeStudents",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<StudentAccountForm>>> loadAllFreeStudents(@RequestBody PagableForm page){
		page.setCondition(this.loadCurrentAccount().getId());
		return returnSuccess(orgBusiness.loadStudentsOutOfOrg(page));
	}
	
	/**
	 * 组织销售商品
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/loadOrgSaleProducts",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> saleProductToStudent(@RequestBody SaleProducesForm sale){
		return returnSuccess(orgBusiness.saleProductToStudent(sale));
	}
	
	@RequestMapping(value = "/loadFreeTeachers",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<DefaultPagingBody<FreeTeacherModule>>> loadFreeTeachers(@RequestBody PagableForm form){
		return returnSuccess(teacherBusiness.loadFreeTeacher(form));
	}
	
	@RequestMapping(value = "/addTeacher2Org",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> addTeacher2Org(@RequestBody AddTeacher2OrgEntity form){
		logInfo(JSONObject.toJSONString(form));
		logInfo(JSONObject.toJSONString(this.loadCurrentAccount()));
		this.orgBusiness.addOrgTeacher(new OrgTeacherForm(form.getOrgId(), form.getTeacherId()));
		return returnSuccess(true);
	}
	
	@RequestMapping(value = "/addStudent2Org",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> addStudent2Org(@RequestBody OrgStudentForm form){
		form.setOrgId(this.loadCurrentAccount().getId());
		return returnSuccess(this.orgBusiness.addOrgStudent(form));
	}
	/**
	 * 组织修改商品
	 * @param user .
	 * @return .
	 */
	@RequestMapping(value = "/updateOrgProduct",consumes = "application/json",method = {RequestMethod.POST })
	@Ignore
	public ResponseEntity<BaseRespEntity<Boolean>> updateOrgProduct(@RequestBody ProductEntity product){
		return returnSuccess(productBusiness.updateOrgProduct(product)!=null);
	}
}
